package com.liangzhong.proxy.user
{
	import com.adobe.serialization.json.JSON;
	import com.common.utils.Debug;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.message.http.WebClient;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.user.UserDetailInfo;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxy.club.ClubProxy;
	import com.liangzhong.proxy.training.TrainingProxy;
	
	import flash.events.Event;
	import flash.net.URLVariables;
	
	import lz.controls.Alert;
	
	/**
	 *用户信息 
	 * @author lxh
	 * 
	 */	
	public class UserProxy extends BaseProxy
	{
		public static const ProxyName :String = "UserProxy";
		
		public static const GetUserInfo_Result	   :String = "Http/User/GetUserInfo_Result";
		
		public function UserProxy(proxyName:String = ProxyName, data:Object=null)
		{
			super(proxyName, data);
		}
		
		/**
		 *请求用户详细信息 
		 */		
		public function requestUserDetailInfo(uid:int,authkey:String,method:String,format:String):void
		{
			////获取用户详细信息
			var webReq :WebClient = new WebClient();
			var callBack :Function = getUserDetailInfo;
			var variables :URLVariables = new URLVariables();
			webReq.resultFormat = format;
			webReq.svrUrl = GlobalDef.Web_Api_Url ;
			webReq.isAddReqWaiting = false;
			variables.uid = uid;
			variables.authkey = authkey;
			variables.method = method;
			variables.format = format;
			webReq.call(callBack,variables);
		}
		/**
		 *个人资料返回 
		 * @param data
		 * 
		 */		
		private function getUserDetailInfo(data :Object):void
		{
			//trace("个人资料：\n"+data);
			var userDetailInfo :UserDetailInfo = AppData.getInstance().user.userDetailInfo;
			userDetailInfo.parse(data);
			this.sendNotification(GetUserInfo_Result,userDetailInfo);
			getClubInfo();
		}
		/**
		 *获得服务器时间后请求其他数据 
		 * 
		 */
		private function getClubInfo():void
		{
//			this.sendNotification(ClubProxy.REQUEST_CLUB_INFO);	//俱乐部信息
		}
		/**
		 *请求商城数据列表 
		 * 
		 */
		public function requestShopList(uid:int,authkey:String,method:String,format:String):void
		{
			var webReq :WebClient = new WebClient();
			var callBack :Function = getShopListRes;
			var variables :URLVariables = new URLVariables();
			webReq.resultFormat = format;
			webReq.svrUrl = GlobalDef.Web_Api_Url ;
			webReq.isAddReqWaiting = false;
			variables.uid = uid;
			variables.authkey = authkey;
			variables.method = method;
			variables.format = format;
			webReq.call(callBack,variables);
		}
		/**
		 *返回商城数据列表 
		 * @param $data
		 * 
		 */
		private function getShopListRes($data:Object):void
		{
			//Debug.echo("商城"+$data);
			var str:String = String($data);
			var results:Array = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().shopDataManager.addEventListener(Event.CHANGE,onrequestBagdata);
				//AppData.getInstance().shopDataManager.getShopData(obj.list);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retecode));
			}
		}
		/**
		 * 请求背包数据
		 * 商场数据请求回来并解析完成 
		 * @param event
		 * 
		 */
		private function onrequestBagdata(event:Event):void
		{
			requestEquipList();
		}
		/**
		 *请求装备信息 
		 * 
		 */		
		private function requestEquipList():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.EQUIP_LIST;
			ProxyUtils.instance.webClient(getEquipList,variables,false);
		}
		private function getEquipList($data:Object):void
		{
			//Debug.echo("背包"+$data);
			var str:String = String($data);
			var results:Array = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
//				AppData.getInstance().bagDataManager.analysisEquipData(obj.list);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retecode));
			}
		}
	}
}